package com.aote.util.HttpClient;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * 提供post和get的http方法
 */
public class HttpRquest {
    static Logger log = Logger.getLogger(HttpRquest.class);

    /**
     * post 参数传的是json  path 路径要发的逻辑路径，json 传参，json字符串，不是普通的字符串
     * */
    public static JSONObject HttpPost(String path, String json) {
        String actual = null;
        JSONObject js = null;
        try {
            HttpPost postMethod = new HttpPost(path);
            StringEntity se = new StringEntity(json, "UTF-8");
            postMethod.setEntity(se);
            // 发送Post请求
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(postMethod);
            if (response.getEntity() != null) {
                actual = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
            js = new JSONObject(actual);
            return js;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return js;
    }

    /**
     * 发送微信需要的数据xml 微信需要的.xml 格式的数据转string，path，请求地址
     */
    public static String HttpxmlPost(String xml, String path) {
        log.debug(xml + path);
        String returnXml = null;
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(path);
            StringEntity postEntity = new StringEntity(xml, "UTF-8");
            postRequest.addHeader("Content-Type", "text/xml");
            postRequest.setEntity(postEntity);
            HttpResponse httpResponse = httpclient.execute(postRequest);
            HttpEntity entity = httpResponse.getEntity();
            log.debug(entity);
            if (entity != null) {
                returnXml = EntityUtils.toString(entity, "UTF-8");
                log.debug(returnXml);
            }
            log.debug("最后的" + returnXml);
            return returnXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.debug("没有的" + returnXml);
        return returnXml;
    }

    public static JSONObject HttpGet(String path) throws UnsupportedEncodingException {
        JSONObject accob = null;
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpGet getReqacc = new HttpGet(path);
            HttpResponse httpResacc = httpclient.execute(getReqacc);
            HttpEntity accent = httpResacc.getEntity();
            if (accent != null) {
                String accentstr = EntityUtils.toString(accent, "utf-8");
                accob = new JSONObject(accentstr);
                return accob;
            }
        } catch (Exception e) {
            log.debug(e.getMessage());
            throw new RuntimeException(e);
        }
        return accob;
    }


}
